﻿#region Using Directives

using System;
using System.ComponentModel;
using Edinger.Components.Forms;
using System.Diagnostics;
using System.Windows.Forms;

#endregion Using Directives


namespace Edinger.Components
{
    internal partial class ForegroundWorker : Component
    {
        #region Fields

        private static readonly object DoWorkEventKey = new object();
        private static readonly object RunWorkerCompletedEventKey = new object();

        private ForegroundWorkerForm form;

        #endregion Fields


        #region Methods

        protected override void Dispose(bool disposing)
        {
            if (disposing )
            {
                if(components != null)
                    components.Dispose();

                if (form != null)
                    form.Dispose();
            }

            base.Dispose(disposing);
        }


        private void mainBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            OnDoWork(e);
        }


        private void mainBackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            form.mainProgressBar.Value = e.ProgressPercentage;
            // form.supplementaryInstructionsLabel.Text = (string)e.UserState;
        }


        private void mainBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (!e.Cancelled)
            {
                form.DialogResult = DialogResult.OK;
                form.Close();
            }

            OnRunWorkerCompleted(e);
        }


        protected virtual void OnDoWork(DoWorkEventArgs e)
        {
            DoWorkEventHandler handler = Events[DoWorkEventKey] as DoWorkEventHandler;
            if (handler != null)
                handler(this, e);
        }


        protected virtual void OnRunWorkerCompleted(RunWorkerCompletedEventArgs e)
        {
            RunWorkerCompletedEventHandler handler = Events[RunWorkerCompletedEventKey] as RunWorkerCompletedEventHandler;
            if (handler != null)
                handler(this, e);
        }


        public void ReportProgress(int percent, string message)
        {
            mainBackgroundWorker.ReportProgress(percent, message);
        }


        public void RunWorker(IWin32Window owner, object argument)
        {
            form.supplementaryInstructionsLabel.Text = null;
            form.mainProgressBar.Value = 0;

            mainBackgroundWorker.RunWorkerAsync(argument);
            if (form.ShowDialog(owner) == DialogResult.Cancel)
                mainBackgroundWorker.CancelAsync();
        }

        #endregion Methods


        #region Properties

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool CancellationPending
        {
            get
            {
                return mainBackgroundWorker.CancellationPending;
            }
        }


        [DefaultValue("")]
        public string Title
        {
            get
            {
                return form.Text;
            }
            set
            {
                form.Text = value;
            }
        }


        [DefaultValue("")]
        public string Description
        {
            get
            {
                return form.mainInstructionsLabel.Text;
            }
            set
            {
                form.mainInstructionsLabel.Text = value;
            }
        }

        #endregion Properties


        #region Events

        public event DoWorkEventHandler DoWork
        {
            add
            {
                Events.AddHandler(DoWorkEventKey, value);
            }
            remove
            {
                Events.RemoveHandler(DoWorkEventKey, value);
            }
        }


        public event RunWorkerCompletedEventHandler RunWorkerCompleted
        {
            add
            {
                Events.AddHandler(RunWorkerCompletedEventKey, value);
            }
            remove
            {
                Events.RemoveHandler(RunWorkerCompletedEventKey, value);
            }
        }

        #endregion Events


        #region Constructors

        public ForegroundWorker()
        {
            InitializeComponent();

            this.form = new ForegroundWorkerForm();
        }


        public ForegroundWorker(IContainer container)
        {
            container.Add(this);

            InitializeComponent();

            this.form = new ForegroundWorkerForm();
        }

        #endregion Constructors
    }
}
